package com.mjlf.mqzook.server;

import com.alibaba.fastjson.JSONObject;
import com.mjlf.mqzook.entity.RegistryServer;
import com.mjlf.mqzook.entity.ZkTopic;
import com.mjlf.mqzook.inter.RegistryFacotry;
import com.mjlf.mqzook.inter.TopicFactroy;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;
import java.util.List;

/**
 * @ClassName TopicServer
 * @Author mjlft
 * @Date 2020/2/18 16:31
 * @Version 1.0
 * @Description TODO
 */
@Service
public class TopicServer implements TopicFactroy {

    @Override
    public JSONObject createTopic(ZkTopic topic) {
        JSONObject jsonObject = new JSONObject();
        if(TopicFactroy.TOPICS.containsKey(topic.getTopicName())){
            jsonObject.put("code", 600);
            jsonObject.put( "msg", "topic已经存在");
            return jsonObject;
        }
        //拿取当前服务
        Set<RegistryServer> registryServers = RegistryFacotry.servers.get("mq-server");
        if(registryServers == null || registryServers.isEmpty()){
            jsonObject.put("code", 700);
            jsonObject.put("msg", "没有可用服务器");
            return jsonObject;
        }

        List<RegistryServer> list = new ArrayList<>(registryServers.size());
        list.addAll(registryServers);
        list.sort((o1, o2) -> {
            return o1.getPartitionNum() > o2.getPartitionNum() ? 1 : 0;
        });
        RegistryServer registryServer = list.get(0);
        topic.setLeader(registryServer.getHost() + ":" + registryServer.getPort());
        registryServer.setPartitionNum(registryServer.getPartitionNum() + 1);

        if(list.size() == 1){
            topic.setFollow(registryServer.getHost() + ":" + registryServer.getPort());
            registryServer.setPartitionNum(registryServer.getPartitionNum() + 1);
        }else {
            registryServer = list.get(1);
            topic.setFollow(registryServer.getHost() + ":" + registryServer.getPort());
            registryServer.setPartitionNum(registryServer.getPartitionNum() + 1);
        }

        TopicFactroy.TOPICS.put(topic.getTopicName(), topic);

        jsonObject.put("code", 200);
        jsonObject.put("data", topic);
        return jsonObject;
    }

    @Override
    public JSONObject deleteTopic(String name) {
        return null;
    }

    public JSONObject releader(String host, Integer port){
        JSONObject jsonObject = new JSONObject();

        Iterator<String> iterator = TOPICS.keySet().iterator();
        while (iterator.hasNext()){
            String topicName = iterator.next();
            ZkTopic zkTopic = TOPICS.get(topicName);
            if(zkTopic.getLeader().equals(host + ":" + port)){
                zkTopic.setLeader(zkTopic.getFollow());
                //拿取当前服务
                Set<RegistryServer> registryServers = RegistryFacotry.servers.get("mq-server");
                List<RegistryServer> list = new ArrayList<>(registryServers.size());
                list.addAll(registryServers);
                list.sort((o1, o2) -> {
                    return o1.getPartitionNum() > o2.getPartitionNum() ? 1 : 0;
                });

                RegistryServer registryServer = list.get(0);
                zkTopic.setFollow(registryServer.getHost() + ":" + registryServer.getPort());
            }
        }

        jsonObject.put("code", 200);
        return jsonObject;
    }

}
